package com.shyroke.cool.controller;

import com.shyroke.cool.bean.ArticlePO;
import com.shyroke.cool.bean.PageDTO;
import com.shyroke.cool.bean.boke.TreeDto;
import com.shyroke.cool.mapper.ArticlePOMapper;
import com.shyroke.cool.service.ArticleService;
import com.shyroke.cool.service.boke.IBokeService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 博客Controller
 */
@Controller
@RequestMapping(value = "bokeController")
public class BoKeController {
    private Logger logger = Logger.getLogger(getClass());


    @Autowired
    private IBokeService bokeService;

    @Autowired
    private ArticleService articleService;

    /**
     *
     * 博客首页
     * @param page
     * @param labelID 文章标签ID
     * @return
     */
    @RequestMapping(value = "bokeMain")
    public ModelAndView bokeMain( PageDTO page, @RequestParam(value = "labelID",required = false)String labelID, ModelAndView model) {
        //TODO 博客内容还没做动态处理，将在第二版本做处理
        logger.debug("=============登录博客==============");

        //分页信息默认为每页5条
        if(page.getPageNo()==0 || page.getPageSize()==0){
            page = new PageDTO();
            page.setPageNo(1);
            page.setPageSize(5);
        }
        List<ArticlePO> articlePOList = null;
        if(StringUtils.isEmpty(labelID)){
            //label如果不为空说明根据标签名来搜索所有文章
            articlePOList =   articleService.getArticleList(page,labelID);
        }else{
            //label为空说明查询所有label中的文章
          articlePOList =   articleService.getArticleList(page,null);

        }

        model.addObject("articleList",articlePOList);
        model.setViewName("boke/main");
        return model;
    }



    /**
     * 获取博客树形节点所需的数据
     * @return
     */
    @RequestMapping(value = "getParentFile")
    @ResponseBody
    public List<TreeDto> getParentFile(){
        logger.debug("=============获取博客树形节点所需的数据=============");
        return bokeService.getParentFile();
    }
}
